/*
 * Copyright (C), 2002-2016, 苏宁易购电子商务有限公司
 * FileName: UserApi.java
 * Author:   admin
 * Date:     2016年3月15日 下午7:50:05
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.app.snpaasadmin.api;

import com.suning.app.snpaasadmin.domain.OauthToken;
import com.suning.app.snpaasadmin.util.CloudFoundryClientUtil;
import com.suning.app.snpaasadmin.util.Constants;
import com.suning.app.snpaasadmin.util.HttpClientUtil;
import com.suning.app.snpaasadmin.util.JsonUtil;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author admin
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class UserApi {
    
    public static Map<Integer, String> SERVICE_STATUS_DSCRB = new HashMap<Integer, String>();

    public static void startApplication(String domain, String uuid, OauthToken token) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("state", Constants.APP_STARTED);
        String urlPath = "http://api." + domain + "/v2/apps/" + uuid + "?async=true&inline-relations-depth=1";
        CloudFoundryClientUtil.doCCPut(urlPath, params, token);
    }

    public static void stopApplication(String domain, String uuid, OauthToken token) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("state", Constants.APP_STOPPED);
        String urlPath = "http://api." + domain + "/v2/apps/" + uuid + "?async=true&inline-relations-depth=1";
        CloudFoundryClientUtil.doCCPut(urlPath, params, token);
    }

    public static void restartApplication(String domain, String uuid, OauthToken token) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("state", Constants.APP_STOPPED);
        String urlPath = "http://api." + domain + "/v2/apps/" + uuid + "?async=true&inline-relations-depth=1";
        CloudFoundryClientUtil.doCCPut(urlPath, params, token);
        params.clear();
        params.put("state", Constants.APP_STARTED);
        CloudFoundryClientUtil.doCCPut(urlPath, params, token);
    }

    public static Map<String, Object> getApp(String domain, String uuid, OauthToken token) throws Exception {
        String urlPath = "http://api." + domain + "/v2/apps/" + uuid + "/summary";
        String content = CloudFoundryClientUtil.doCCGet(urlPath, null, token);
        return JsonUtil.convertJsonToMap(content);
    }
    
    public static void setSERVICE_STATUS_DSCRB() {

        SERVICE_STATUS_DSCRB.put(1, "Subscribing Product");
        SERVICE_STATUS_DSCRB.put(2, "Subcribe Product Successfully");
        SERVICE_STATUS_DSCRB.put(3, "Subcribe Product Failure");
        // 续费
        SERVICE_STATUS_DSCRB.put(4, "Renewing Product");
        SERVICE_STATUS_DSCRB.put(5, "Renew Product Successfully");
        SERVICE_STATUS_DSCRB.put(6, "Renew Product Failure");
        // 升级
        SERVICE_STATUS_DSCRB.put(7, "Upgrading Product");
        SERVICE_STATUS_DSCRB.put(8, "Upgrade Product Successfully");
        SERVICE_STATUS_DSCRB.put(9, "Upgrade Product Failure");
        // 退订
        SERVICE_STATUS_DSCRB.put(10, "Unsubscribe Product Successfully");
        SERVICE_STATUS_DSCRB.put(11, "Unsubscribe Product Failure");
        // 到期
        SERVICE_STATUS_DSCRB.put(12, "Product Expired");
        // 销毁
        SERVICE_STATUS_DSCRB.put(13, "Product Destroyed");
        // IAAS
        // 销毁
        SERVICE_STATUS_DSCRB.put(14, "Product Destroyed_ing");
        SERVICE_STATUS_DSCRB.put(15, "Product Destroyed Failure");
        // 退订
        SERVICE_STATUS_DSCRB.put(16, "Unsubscribing Product");
        // 到期
        SERVICE_STATUS_DSCRB.put(17, "Product Expired_ing");
        SERVICE_STATUS_DSCRB.put(18, "Product Expired");
        // 停止
        SERVICE_STATUS_DSCRB.put(24, "Suspending Product ");
        SERVICE_STATUS_DSCRB.put(25, "Suspend Product Successfully");
        SERVICE_STATUS_DSCRB.put(26, "Suspend Product Failure");
        // 恢复
        SERVICE_STATUS_DSCRB.put(27, "Regaining Product ");
        SERVICE_STATUS_DSCRB.put(28, "Regain Product Successfully");
        SERVICE_STATUS_DSCRB.put(29, "Regain Product Failure");
        // 释放
        SERVICE_STATUS_DSCRB.put(210, "Releaseing Product ");
        SERVICE_STATUS_DSCRB.put(211, "Release Product Successfully");
        SERVICE_STATUS_DSCRB.put(212, "Release Product Failure");

    }

    public static Map<String, String> getServiceStatus(String instanceID, String memberId, int checkResPay) {
        setSERVICE_STATUS_DSCRB();
        String url = null;
        Properties prop = new Properties();
        InputStream in = UserApi.class.getResourceAsStream("/snpaasadmin.properties");

        try {
            prop.load(in);
            url = prop.getProperty("SYS.QUERYURL").trim();
        } catch (Exception e) {
            e.printStackTrace();
        }

        String params = "instanceID=" + instanceID + "&memberId=" + memberId + "&checkResPay=" + checkResPay;

        String response = "";
        Map<String, String> rst = new HashMap<String, String>();
        try {

            response = HttpClientUtil.sendPostToYunInterface(url, params);

        } catch (Exception e) {
            rst.put("success", "false");
            rst.put("result_msg", "The query interface is not avliable ");
            e.printStackTrace();
            return rst;
        }

        if (response.equals("")) {
            rst.put("success", "false");
            rst.put("result_msg", "not data from the query status interface");
            return rst;
        }

        Map<String, Object> respnse_mp = JsonUtil.convertJsonToMap(response);

        rst.put("success", respnse_mp.get("success").toString());

        if (!(boolean) respnse_mp.get("success")) {
            rst.put("result_msg", respnse_mp.get("msg").toString());
            return rst;
        }

        int state = (int) (respnse_mp.get("state"));
        String product_state_dscb = SERVICE_STATUS_DSCRB.get(state);
        rst.put("result_code", String.valueOf(state));
        rst.put("result_msg", product_state_dscb);
        rst.put("endDate", respnse_mp.get("endDate").toString());
        rst.put("resMoney", String.valueOf(respnse_mp.get("resMoney")));
        return rst;
    }

}
